package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 农机主 找驾驶员
 */
public class NjDirverListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DIrverOrderBean> dIrverOrderBeanList;
    private Context context;
    private RecyclerViewItemClickLisenter<DIrverOrderBean> onItemClickLisenter;
    private DirverOrderCzCallBack dirverOrderCzCallBack;

    public void setDirverOrderCzCallBack(DirverOrderCzCallBack dirverOrderCzCallBack) {
        this.dirverOrderCzCallBack = dirverOrderCzCallBack;
    }

    public void setOnItemClickLisenter(RecyclerViewItemClickLisenter<DIrverOrderBean> onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DIrverOrderBean dIrverOrderBean= (DIrverOrderBean) view.getTag();
            if (view.getId()==R.id.tv_caozuo){
                dirverOrderCzCallBack.caozuo(dIrverOrderBean);
            }else {
                onItemClickLisenter.onItemClickLisenter(dIrverOrderBean);
            }

        }
    };

    public NjDirverListAdapter(List<DIrverOrderBean> dIrverOrderBeanList, Context context) {
        this.dIrverOrderBeanList = dIrverOrderBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_find_dirver,null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder= (MyHolder) holder;
        DIrverOrderBean dIrverOrderBean=dIrverOrderBeanList.get(position);
        myHolder.tvNumber.setText(dIrverOrderBean.getMeshNum());
        myHolder.tvTIme.setText(dIrverOrderBean.getCreateTime());
        myHolder.tvStart.setText(dIrverOrderBean.getBeginDate());
        myHolder.tvEnd.setText(dIrverOrderBean.getEndDate());
        myHolder.tvType.setText(dIrverOrderBean.getMeshTypeName());
        myHolder.tvAddress.setText(dIrverOrderBean.getWorkAddress());
        myHolder.tvPrice.setText(dIrverOrderBean.getWorkPrice());
        myHolder.tvCz.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
        if (0==dIrverOrderBean.getOrderStatus()){
            myHolder.tvCz.setText("已取消");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
        }else if (1==dIrverOrderBean.getOrderStatus()){
            if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                myHolder.tvCz.setText("立即接单");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
            }else {
                myHolder.tvCz.setText("待接单");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }
        }else if (2==dIrverOrderBean.getOrderStatus()){
            if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                myHolder.tvCz.setText("待移交");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }else {
                myHolder.tvCz.setText("移交农机设备");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
            }
        }else if (3==dIrverOrderBean.getOrderStatus()){
            if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                myHolder.tvCz.setText("确认");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_3));
            }else {
                myHolder.tvCz.setText("待农机驾驶员确认..");
                myHolder.tvCz.setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }
        }else if (4==dIrverOrderBean.getOrderStatus()){
            if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                myHolder.tvCz.setText("使用中");
                myHolder.tvCz.setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }else {
                myHolder.tvCz.setText("待归还");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }

        }else if (5==dIrverOrderBean.getOrderStatus()){
            if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                myHolder.tvCz.setText("归还待确认");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
            }else {
                myHolder.tvCz.setText("确认归还");
                myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_radius_4_blue));
            }
        }else if (6==dIrverOrderBean.getOrderStatus()){
            myHolder.tvCz.setText("已归还");
            myHolder.tvCz.setBackground(context.getResources().getDrawable(R.drawable.bg_dirver_4));
        }
        myHolder.tvCz.setTag(dIrverOrderBean);
        myHolder.tvCz.setOnClickListener(onClickListener);
        myHolder.itemView.setTag(dIrverOrderBean);
        myHolder.itemView.setOnClickListener(onClickListener);

    }

    @Override
    public int getItemCount() {
        return dIrverOrderBeanList.size();
    }

    public  interface DirverOrderCzCallBack{
        void caozuo(DIrverOrderBean dIrverOrderBean);
    }

    public class MyHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_number_tv)
        TextView tvNumber;
        @BindView(R.id.item_time_tv)
        TextView tvTIme;
        @BindView(R.id.item_type_tv)
        TextView tvType;
        @BindView(R.id.item_start_tv)
        TextView tvStart;
        @BindView(R.id.item_end_tv)
        TextView tvEnd;
        @BindView(R.id.item_address_tv)
        TextView tvAddress;
        @BindView(R.id.item_money_tv)
        TextView tvPrice;
        @BindView(R.id.tv_caozuo)
        TextView tvCz;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
